001 /*
002 * Copyright 2005 Stephen McConnell
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.transit.tools;
020
021 import java.util.LinkedList;
022 import java.util.prefs.Preferences;
023
024 import org.apache.tools.ant.BuildException;
025 import org.apache.tools.ant.Task;
026
027 /**
028 * Task supports the creation of preference as readable xml files.
029 *
030 * @author <a href="http://www.dpml.net">The Digital Product Meta Library</a>
031 * @version 1.0.0
032 */
033 public class NodeTask extends Task
034 {
035 private String m_name;
036 private LinkedList m_nodes = new LinkedList();
037 private LinkedList m_entries = new LinkedList();
038
039 /**
040 * Set the preference node name.
041 * @param name the node name
042 */
043 public void setName( String name )
044 {
045 m_name = name;
046 }
047
048 /**
049 * Create and return a new nested node.
050 * @return the new node
051 */
052 public NodeTask createNode()
053 {
054 NodeTask node = new NodeTask();
055 m_nodes.add( node );
056 return node;
057 }
058
059 /**
060 * Create and return a new attribute entry.
061 * @return the new attribute
062 */
063 public EntryTask createEntry()
064 {
065 EntryTask entry = new EntryTask();
066 m_entries.add( entry );
067 return entry;
068 }
069
070 void apply( Preferences prefs )
071 {
072 EntryTask[] entries = (EntryTask[]) m_entries.toArray( new EntryTask[0] );
073 for( int i=0; i<entries.length; i++ )
074 {
075 EntryTask entry = entries[i];
076 entry.apply( prefs );
077 }
078 NodeTask[] nodes = (NodeTask[]) m_nodes.toArray( new NodeTask[0] );
079 for( int i=0; i<nodes.length; i++ )
080 {
081 NodeTask node = nodes[i];
082 String name = node.getNodeName();
083 Preferences child = prefs.node( name );
084 node.apply( child );
085 }
086 }
087
088 private String getNodeName()
089 {
090 if( null == m_name )
091 {
092 final String error =
093 "Missing name attribute.";
094 throw new BuildException( error, getLocation() );
095 }
096 return m_name;
097 }
098
099 /**
100 * An attribute entry.
101 */
102 public class EntryTask extends Task
103 {
104 private String m_key;
105 private Object m_value;
106 private Preferences m_prefs;
107 private Class m_type;
108
109 /**
110 * Set the attribute key.
111 * @param key the attribute name
112 */
113 public void setKey( String key )
114 {
115 m_key = key;
116 }
117
118 /**
119 * Set the attribute string value.
120 * @param value the string value
121 */
122 public void setValue( String value )
123 {
124 checkValue();
125 m_value = value;
126 }
127
128 /**
129 * Set the attribute as a boolean value.
130 * @param value the boolean value
131 */
132 public void setBoolean( boolean value )
133 {
134 checkValue();
135 m_value = new Boolean( value );
136 }
137
138 /**
139 * Set the attribute as a int value.
140 * @param value the int value
141 */
142 public void setInt( int value )
143 {
144 checkValue();
145 m_value = new Integer( value );
146 }
147
148 /**
149 * Set the attribute as a double value.
150 * @param value the double value
151 */
152 public void setDouble( double value )
153 {
154 checkValue();
155 m_value = new Double( value );
156 }
157
158 /**
159 * Set the attribute as a float value.
160 * @param value the float value
161 */
162 public void setFloat( float value )
163 {
164 checkValue();
165 m_value = new Float( value );
166 }
167
168 private void checkValue()
169 {
170 if( null != m_value )
171 {
172 final String error =
173 "Entry value is already assigned to an instance of "
174 + m_value.getClass().getName()
175 + ".";
176 throw new BuildException( error, getLocation() );
177 }
178 }
179
180 void apply( Preferences prefs )
181 {
182 if( null == m_key )
183 {
184 final String error =
185 "Missing key attribute.";
186 throw new BuildException( error, getLocation() );
187 }
188 if( null == m_value )
189 {
190 final String error =
191 "Missing value attribute.";
192 throw new BuildException( error, getLocation() );
193 }
194 Class c = m_value.getClass();
195 if( c == String.class )
196 {
197 prefs.put( m_key, (String) m_value );
198 }
199 else if( c == Boolean.class )
200 {
201 prefs.putBoolean( m_key, ( (Boolean) m_value ).booleanValue() );
202 }
203 else if( c == Integer.class )
204 {
205 prefs.putInt( m_key, ( (Integer) m_value ).intValue() );
206 }
207 else if( c == Double.class )
208 {
209 prefs.putDouble( m_key, ( (Double) m_value ).doubleValue() );
210 }
211 else if( c == Float.class )
212 {
213 prefs.putFloat( m_key, ( (Float) m_value ).floatValue() );
214 }
215 }
216 }
217 }